package cn.autowired.exam.controller;

import cn.autowired.exam.base.CommonResponse;
import cn.autowired.exam.dto.PaperDto;
import cn.autowired.exam.entity.Question;
import cn.autowired.exam.entity.StudentPaper;
import cn.autowired.exam.service.QuestionService;
import cn.autowired.exam.service.StudentPaperService;
import com.google.common.collect.Maps;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import cn.autowired.exam.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.autowired.exam.service.PaperService;

import cn.autowired.exam.entity.Paper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 *
 *   @description : Paper 控制器
 *   ---------------------------------
 */
@RestController
@RequestMapping("/paper")
public class PaperController extends AbstractController {
private final Logger logger=LoggerFactory.getLogger(PaperController.class);

    @Autowired
    public PaperService paperService;

    @Autowired
    public QuestionService questionService;

    @Autowired
    public StudentPaperService studentPaperService;

    /**
     * 根据试卷id获取完整试卷
     * @param paperId 试卷id
     * @return 包含题目的实体类
     */
    @GetMapping("/getPaper/{paperId}")
    public ResponseEntity getPaperById(@PathVariable("paperId") int paperId){
        PaperDto paperDto = new PaperDto();
        paperDto.setId(paperId);
        Paper paper = paperService.selectById(paperId);
        paperDto.setTitle(paper.getTitle());
        paperDto.setType(paper.getType());
        LinkedList<Question> questionList = new LinkedList<>();
        List<Question> questions = questionService.selectList(new EntityWrapper<Question>()
                .eq(Question.Field.paperId, paperId));
        if (questions.size()!=0){
            for (Question question : questions) {
                questionList.add(question);
            }
        }
        paperDto.setQuestionList(questionList);
        return ResponseEntity.ok(paperDto);
    }

    /**
     * 获取试卷列表
     * @return
     */
    @GetMapping("/getPaperList")
    public ResponseEntity getPaperList(){
        List<Paper> papers = paperService.selectList(new EntityWrapper<>());
        HashMap<Object, Object> hashMap = Maps.newHashMap();
        hashMap.put("msg","success");
        hashMap.put("code",0);
        hashMap.put("count",papers.size());
        hashMap.put("data",papers);
        return ResponseEntity.ok(hashMap);
    }

    /**
     * 获取试卷列表减去指定学生已经预约过的考试列表
     * @return
     */
    @GetMapping("/getPaperListOfStudentNo")
    public ResponseEntity getPaperListOfStudent(String studentNo){
        List<Paper> papers = paperService.selectList(new EntityWrapper<>());
        List<StudentPaper> studentPapers = studentPaperService.selectList(new EntityWrapper<StudentPaper>().eq(StudentPaper.Field.studentNo, studentNo));

        //集合删除元素只能用迭代器的方式删除
        Iterator<Paper> it = papers.iterator();
        while(it.hasNext()){
            Paper paper = it.next();
            for (StudentPaper studentPaper : studentPapers) {
                if(paper.getId().equals(studentPaper.getPaperId())){
                    it.remove();
                }
            }

        }

        return ResponseEntity.ok(CommonResponse.ok(papers));
    }


    /**
     * 根据试卷id获取完整试卷
     * @param paperId 试卷id
     * @return 包含题目的实体类
     */
    @GetMapping("/getQuestionList/{paperId}")
    public ResponseEntity getQuestionListById(@PathVariable("paperId") int paperId){
        PaperDto paperDto = new PaperDto();
        paperDto.setId(paperId);
        Paper paper = paperService.selectById(paperId);
        paperDto.setTitle(paper.getTitle());
        paperDto.setType(paper.getType());
        LinkedList<Question> questionList = new LinkedList<>();
        List<Question> questions = questionService.selectList(new EntityWrapper<Question>()
                .eq(Question.Field.paperId, paperId));
        if (questions.size()!=0){
            for (Question question : questions) {
                questionList.add(question);
            }
        }
        HashMap<Object, Object> hashMap = Maps.newHashMap();
        paper.setQuestionCount(questionList.size());
        paper.updateById();
        hashMap.put("msg","success");
        hashMap.put("code",0);
        hashMap.put("count",questionList.size());
        hashMap.put("data",questionList);
        return ResponseEntity.ok(hashMap);
    }
    /**
     *  删除试卷
     */
    @GetMapping("/deleteById")
    public ResponseEntity deletePaper(Integer id){
        boolean flag = paperService.deleteById(id);
        HashMap<Object, Object> hashMap = Maps.newHashMap();
        if (flag){
            hashMap.put("msg","success");
            hashMap.put("code",0);
            return ResponseEntity.ok(hashMap);
        } else {
            hashMap.put("msg","error");
            hashMap.put("code",-1);
            return ResponseEntity.ok(hashMap);
        }
    }

    /**
     * 获取所有的试卷
     * @return
     */
    @GetMapping("/getPapers")
    public ResponseEntity getPaperById(){
        List<Paper> paperList = paperService.selectList(new EntityWrapper<>());
        PaperDto paperDto = new PaperDto();
        for (Paper paper : paperList) {
            paperDto.setTitle(paper.getTitle());
            paperDto.setType(paper.getType());
            paperDto.setId(paper.getId());
        }
        LinkedList<Question> questionList = new LinkedList<>();
        List<Question> questions = questionService.selectList(new EntityWrapper<Question>()
                .eq(Question.Field.paperId, 2));
        if (questions.size()!=0){
            for (Question question : questions) {
                questionList.add(question);
            }
        }
        paperDto.setQuestionList(questionList);
        return ResponseEntity.ok(paperDto);
    }

    /**
     * 创建或更新试卷
     * @param title 标题
     * @param author 作者
     * @return 成功 200 返回success or失败 400 不返回值
     */
    @PostMapping("/addOrUpdate")
    public ResponseEntity addPaper(String title,String author){
        Paper paper = new Paper();
        paper.setAuthor(author);
        paper.setTitle(title);
        boolean isInsert = paper.insertOrUpdate();
        return isInsert?ResponseEntity.ok("success"):ResponseEntity.badRequest().build();
    }


}